package DAO;

import javabean.Admin;

import javax.servlet.http.HttpSession;
import java.sql.*;

public class AdminDAO implements IAdminDAO{
    @Override
    public boolean login(Admin admin) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        boolean success = false;
        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("SELECT * FROM Admin WHERE AdminID = ? AND Password = ?");
            statement.setString(1, admin.getAdminID());
            statement.setString(2, admin.getPassword());
            resultSet = statement.executeQuery();
            success = resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }
        return success;
    }

    @Override
    public Admin getAdmin(String adminID) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Admin admin = null;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareStatement("SELECT * FROM Admin WHERE AdminID = ?");
            statement.setString(1, adminID);
            resultSet = statement.executeQuery();

            if (resultSet.next()) {
                admin = new Admin();
                admin.setAdminID(resultSet.getString("AdminID"));
                admin.setAdminName(resultSet.getString("AdminName"));
                admin.setAdminPhone(resultSet.getString("AdminPhone"));
                // 设置其他管理员属性
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, resultSet);
        }
        return admin;
    }
    @Override
    public boolean pendingStaff(int pendingID, String adminID) {
        Connection connection = null;
        CallableStatement statement = null;
        boolean success = false;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareCall("{CALL ApprovePendingStaff(?, ?)}");
            statement.setInt(1, pendingID);
            statement.setString(2, adminID);

            statement.executeUpdate();
            success = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, null);
        }

        return success;
    }
    @Override
    public boolean pendingTechnician(int pendingID, String adminID) {
        Connection connection = null;
        CallableStatement statement = null;
        boolean success = false;

        try {
            connection = ConnectManager.getConnection();
            statement = connection.prepareCall("{CALL ApprovePendingTechnician(?, ?)}");
            statement.setInt(1, pendingID);
            statement.setString(2, adminID);

            statement.executeUpdate();
            success = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            ConnectManager.closeConnection(connection, statement, null);
        }

        return success;
    }
}
